import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: () => import("@/views/Index"), meta: { title: "首页", needLogin: false,isTab:true } },
  { path: '/menu', component: () => import("@/views/Menu"), meta: { title: "分类", needLogin: false, isTab: true } },
  { path: '/car', component: () => import("@/views/Car"), meta: { title: "购物车", needLogin: false, isTab: true } },
  { path: '/user', component: () => import("@/views/User"), meta: { title: "用户", needLogin: false, isTab: true } },
  { path: '/user/address', component: () => import("@/views/Address"), meta: { title: "用户地址", needLogin: true } },
  { path: '/login', component: () => import("@/views/Login"), meta: { title: "登录", needLogin: false } },
  { path: '/reg', component: () => import("@/views/Reg"), meta: { title: "注册", needLogin: false } },
  { path: '/goodlist/:id', component: () => import("@/views/List"), meta: { title: "商品列表", needLogin: false } },
  { path: '/goodinfo/:id', component: () => import("@/views/Good"), meta: { title: "商品详情", needLogin: false } },
  { path: '/*', component: () => import("@/views/NotFound"), meta: { title:"404",needLogin:false }  },
]

const router = new VueRouter({
  routes
})


// 拦截器
router.beforeEach((to,from,next)=>{
  document.title = to.meta.title;
  if(to.meta.needLogin){
    // 是否已经登录
    if(sessionStorage.getItem("userinfo")){
      next();
    }else{
      next("/login")
    }
  }else{
     next()
  }
})

export default router
